'use strict';

const Controller = require('./base');

class ActController extends Controller {

  // 界面列表
  async list() {
    const ctx = this.ctx;
    ctx.set('Content-Type', 'application/json');
    const list = await ctx.service.act.list(ctx.query);
    this.success(list);
  }

  // 界面列表
  async act() {
    const ctx = this.ctx;
    var params = ctx.params;
    ctx.set('Content-Type', 'application/json');
    if (!params.id) {
      this.ctx.throw(500, '参数错误');
    }
    const act = await ctx.service.act.find(params.id);
    this.success(act);
  }

  // 创建界面
  async create() {
    const ctx = this.ctx;
    var post = ctx.request.body;
    ctx.set('Content-Type', 'application/json');
    if (!post.title || !post.desc || !post.member || !post.type) {
      this.ctx.throw(500, '参数错误');
      return;
    }
    // 如果给了ID，就是更新，没有则添加
    if (!post.id) {
      const act = await ctx.service.act.create(post);
      this.success(act);
    } else {
      const act = await ctx.service.act.update(post);
      this.success(act);
    }
  }

  // 删除界面
  async del() {
    const ctx = this.ctx;
    const id = ctx.request.body.id;
    ctx.set('Content-Type', 'application/json');
    const created = await ctx.service.act.del(id);
    this.success({
      msg: "删除成功"
    })
  }

  // 投票
  async vote() {
    const ctx = this.ctx;
    const post = ctx.request.body;
    if (post.uid == undefined || post.aid == undefined) {
      this.ctx.throw(500, '参数错误');
      return;
    }
    ctx.set('Content-Type', 'application/json');
    console.log(typeof (post.aid));
    // ActIds.forEach(item => {
    //   var obj = {
    //     uid: post.uid,
    //     aid: item
    //   };
    //   console.log(item);
    //   const created = await ctx.service.act.vote(obj);
    // });
    const created = await ctx.service.act.vote(post);
    this.success({
      msg: "投票成功"
    })
  }
}

module.exports = ActController;
